package http

import (
	"bytes"
	"io"
	"io/ioutil"
	"net"
	"net/http"

	"time"
)

var httpTransport *http.Transport

func init() {
	httpTransport = &http.Transport{
		MaxIdleConns:    50,
		MaxConnsPerHost: 200,
		Dial: func(netw, addr string) (net.Conn, error) {
			conn, err := net.DialTimeout(netw, addr, time.Second*3) // 设置建立连接超时
			if err != nil {
				return nil, err
			}
			return conn, nil
		},
	}
}

func PostTEXT(URL string, content []byte) (int, []byte, error) {
	return httpRequest("POST", URL, "text/plain", content)
}

func PostJSON(URL string, content []byte) (int, []byte, error) {
	return httpRequest("POST", URL, "application/json", content)
}

func PostFORM(URL string, content []byte) (int, []byte, error) {
	return httpRequest("POST", URL, "application/x-www-form-urlencoded", content)
}

func Get(URL string) (int, []byte, error) {
	return httpRequest("GET", URL, "", nil)
}

func Delete(URL string) (int, []byte, error) {
	return httpRequest("DELETE", URL, "", nil)
}

func httpRequest(method, URL, contentType string, content []byte) (int, []byte, error) {
	client := &http.Client{
		Transport: httpTransport,
		Timeout:   time.Minute,
	}
	// 很坑: go 传参时 nil(或接口变量) 与 nil值的指针变量不一定相等
	// var reader *strings.Reader = nil
	var reader io.Reader = nil
	if content != nil {
		reader = bytes.NewReader(content)
	}
	req, err := http.NewRequest(method, URL, reader)
	if err != nil {
		return 0, nil, err
	}
	if req.Body != nil {
		defer req.Body.Close()
	}

	if contentType != "" {
		req.Header.Add("Content-Type", contentType)
	}
	// req.Header.Add("Referer", URL)
	// req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
	// req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 QBCore/3.53.1159.400 QQBrowser/9.0.2524.400 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat")

	response, err := client.Do(req)
	if err != nil {
		return 0, nil, err
	}
	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)

	return response.StatusCode, body, err
}

// func PostForm(URL string, params map[string]string) (string, error) {
// 	pairs := url.Values{}
// 	for name, value := range params {
// 		pairs.Set(name, value)
// 	}
// 	req, err := http.PostForm(URL, pairs)
// 	if err != nil {
// 		return "", err
// 	}
// 	defer req.Body.Close()
// 	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
// 	// req.Header.Add("Referer", URL)
// 	// req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
// 	// req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 QBCore/3.53.1159.400 QQBrowser/9.0.2524.400 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat")
// 	body, err := ioutil.ReadAll(req.Body)
// 	if err != nil {
// 		return "", err
// 	}
// 	return string(body), nil
// }

// func Get(url string) (string, error) {
// 	client := &http.Client{Timeout: 300 * time.Second}
// 	response, err := client.Get(url)
// 	if err != nil {
// 		return "", err
// 	}
// 	defer response.Body.Close()
// 	body, err := ioutil.ReadAll(response.Body)
// 	if err != nil {
// 		return "", err
// 	}
// 	if response.StatusCode != 200 {
// 		return string(body), fmt.Errorf("HTTP %s", response.Status)
// 	}
// 	return string(body), nil
// }
